package tw.com.softleader.fire.setting.web;

import java.util.List;

import org.joda.time.LocalDateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import tw.com.softleader.fire.setting.entity.AcuAddrEntity;
import tw.com.softleader.fire.setting.service.AcuAddrService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @Author:Rhys
 */

public class AcuAddrAction extends ActionSupport {
	
	@Autowired
	private AcuAddrService service;

	private AcuAddrEntity model;
	public AcuAddrEntity getModel() {
		return model;
	}
	public void setModel(AcuAddrEntity model) {
		this.model = model;
	}


	private List<AcuAddrEntity> models;

	public List<AcuAddrEntity> getModels() {
		return models;
	}

	public Logger getLog() {
		return log;
	}

	public void setLog(Logger log) {
		this.log = log;
	}

	private Logger log = LoggerFactory.getLogger(AddrZipAction.class);
	
	private String searchBox;
	public String getSearchBox() {
		return searchBox;
	}
	public void setSearchBox(String searchBox) {
		this.searchBox = searchBox;
	}
	
	@Override
	public void validate() {
		if (model!=null) {
			if (model.getAcuNo()!=null && model.getAcuNo().length() > 10) {
				addFieldError("InputFail", "Textfield Overflow : AcuNo must be less than 10 characters");
			}
			
			if (model.getAddrCode()!=null && model.getAddrCode().length() > 10) {
				addFieldError("InputFail", "Textfield Overflow : AddrCode must be less than 10 characters");
			}
			
			if (model.getAddrCode()!=null && model.getRodCode().length() > 10) {
				addFieldError("InputFail", "Textfield Overflow : RodCode must be less than 10 characters");
			}
			
			if (model.getAddeTxt()!=null && model.getAddeTxt().length() > 200) {
				addFieldError("InputFail", "Textfield Overflow : AddeTxt must be less than 200 characters");
			}
			
			if (model.getAcuSt()!=null && model.getAcuSt().length() > 10) {
				addFieldError("InputFail", "Textfield Overflow : AcuSt must be less than 10 characters");
			}
			
		}
		
	}

	public String execute() {
		log.debug("execute AcuAddrAction");

		models = service.getAll();
		log.debug("Models = {}", models);
		
		return SUCCESS;
	}
	
	public String search() {
		log.debug("execute AddrZipAction");
		log.debug("Model = {}", model);
		
		model = new AcuAddrEntity();
		model.setAcuNo(this.searchBox);
		models = service.search(model);
		
		log.debug("Models = {}", models);
		
		return SUCCESS;
	}
	
	public String delete() {
		log.debug("do delete");
		log.debug("Model = {}", model);
		try {
			service.delete(model);
		} catch (Exception e) {
			log.debug("!!AcuAddrAction deleteFail!!");
			addFieldError("QueryFail", "Delete Fail : Target already delete");
		}

		models = service.getAll();
		
		return SUCCESS;
	}
	
	public String update() {
		log.debug("do update");
		model.setModifier("guest");
		model.setModifiedTime(LocalDateTime.now());
		log.debug("Model = {}", model);
		try {
			service.update(model);
		} catch (Exception e) {
			log.debug("!!AcuAddrAction updateFail!!");
			addFieldError("QueryFail","Update Fail : update failed");
		}

		models = service.getAll();
		
		return SUCCESS;
	}
	
	public String insert() {
		log.debug("do insert");
		
		model.setCreator("guest");
		model.setModifier("guest");
		model.setCreateTime(LocalDateTime.now());
		model.setModifiedTime(LocalDateTime.now());
		
		log.debug("Model = {}", model);
		try {
			service.insert(model);
		} catch (Exception e) {
			log.debug("!!AcuAddrAction insertFail!!");
			addFieldError("QueryFail","Insert Fail : AcuNo must be unique");
		}
		
		models = service.getAll();
		
		return SUCCESS;
	}

		
}
